package com.smart_campus.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.smart_campus.sys.common.DataGridView;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.smart_campus.sys.domain.Dept;
import com.smart_campus.sys.mapper.DeptMapper;
import com.smart_campus.sys.service.DeptService;

import java.util.List;

/**
 * @author Alfred
 * @date 2020/6/12 11:31
 */
@Service
public class DeptServiceImpl implements DeptService{

    @Resource
    private DeptMapper deptMapper;

    /**
     *
     * 根据id删除
     * @param id
     * @return
     * @CachEvict 的作用 主要针对方法配置，能够根据一定的条件对缓存进行清空
     */
    @CacheEvict(cacheNames = "com.smart_campus.sys.service.impl.DeptServiceImpl",key = "#id")
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return deptMapper.deleteByPrimaryKey(id);
    }


    @Override
    public int insert(Dept record) {
        return deptMapper.insert(record);
    }
    /**
     * 添加部门
     * @param dept
     * @return
     * @CachePut 注释，这个注释可以确保方法被执行，同时方法的返回值也被记录到缓存中，实现缓存与数据库的同步更新
     */
    @CachePut(cacheNames = "com.smart_campus.sys.service.impl.DeptServiceImpl",key = "#result.id")
    @Override
    public Dept insertSelective(Dept dept) {
        this.deptMapper.insertSelective(dept);
        return dept;
    }

    @Override
    public Dept selectByPrimaryKey(Integer id) {
        return deptMapper.selectByPrimaryKey(id);
    }
    /**
     * 修改
     * @param dept
     * @return
     * @CachePut 注释，这个注释可以确保方法被执行，同时方法的返回值也被记录到缓存中，实现缓存与数据库的同步更新
     */
    @CachePut(cacheNames = "com.smart_campus.sys.service.impl.DeptServiceImpl",key = "#result.id")
    @Override
    public Dept updateByPrimaryKeySelective(Dept dept) {
        Dept selectById = this.deptMapper.selectById(dept.getId());
        BeanUtil.copyProperties(dept,selectById, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        this.deptMapper.updateByPrimaryKey(selectById);
        return selectById;
    }


    @Override
    public int updateByPrimaryKey(Dept dept) {
      return deptMapper.updateByPrimaryKey(dept);
    }

    @Override
    public DataGridView queryAllDept(Dept dept) {
        List<Dept> depts = this.deptMapper.queryAllDept(dept);
        return new DataGridView(Long.valueOf(depts.size()),depts);
    }

    /**
     * 查询最大的排序码
     * @return
     */
    @Override
    public Integer queryDeptMaxOrderNum() {
        return this.deptMapper.queryDeptMaxOrderNum();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     * @Cacheable 的作用 主要针对方法配置，能够根据方法的请求参数对其结果进行缓存
     */
    @Cacheable(cacheNames = "com.smart_campus.sys.service.impl.DeptServiceImpl",key = "#id")
    @Override
    public Dept getById(Integer id) {
        return this.deptMapper.getById(id);
    }

    @Override
    public Integer getDeptChildrenCountById(Integer id) {
        return this.deptMapper.getDeptChildrenCountById(id);
    }

    @Override
    public DataGridView getAllDept() {
       List<Dept>depts= this.deptMapper.getAllDept();
       return new DataGridView(depts);
    }

}
